package com.hy.convenient.api.controller.down;

import com.hy.convenient.api.pojo.check.CheckUpdateCheckPojo;
import com.hy.convenient.api.server.CheckUpdateServer;
import com.hy.convenient.common.LayuiToTable;
import com.hy.convenient.common.TableNameEnum;
import com.hy.convenient.tool.MD5;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 用于用户角色的数据下载
 * @className UserDownController
 * @Author hdy
 * @Date 2020-12-24 8:25
 * @Version 1.0
 */
@RestController
public class CheckUpdateController {
    @Autowired
    private CheckUpdateServer checkUpdateServer;

    /**
     * 用于检查是否有表需要更新
     * @param t
     * @return
     */
    @PostMapping("/api/checkUpdateTableName")
    public Map<String, Object> downData(@RequestBody CheckUpdateCheckPojo t) {
//        if (!StringUtils.isEmpty(t.getTableName())) {
//            return LayuiToTable.business_fail("更新表名不能为空");
//        }
//        String str = checkUpdate(t);
//        if (!StringUtils.isEmpty(str)) {
//            return LayuiToTable.business_fail(str);
//        }
//        if (!MD5.check(t.getMerId() + t.getPage() + t.getCwbm(), t.getSign())) {
//            return LayuiToTable.business_fail("签名失败");
//        }
        /**
         * 用于查询有多少数据需要更新
         */
        Map<String, Object> map = new HashMap();
        map.put("merId", t.getMerId());
        map.put("lastUpateTime", t.getLastTime());
        map.put("nowTime", t.getNowTime());
        map.put("lsdh", t.getCwbm());
        List<String> list = checkUpdateServer.queryTableName(map);
        List<Integer> tableName =new ArrayList<>();
        list.forEach(c-> tableName.add(TableNameEnum.valueOf(c).getVal()));
        return LayuiToTable.layuiToTable(tableName.stream().sorted().collect(Collectors.toList()), tableName.size());
    }

    public String checkUpdate(CheckUpdateCheckPojo t) {
        String str = "";
        if (StringUtils.isEmpty(t.getMerId())) {
            str += "商户号不能为空！";
        }
        if (StringUtils.isEmpty(t.getCwbm())) {
            str += "仓位编码不能为空！";
        }
        if (StringUtils.isEmpty(t.getSign())) {
            str += "签名不能为空！";
        }
        return str;
    }

}
